<?php
namespace Com\Pay;

class WeifuPay{

    private $config = array();

  
    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info,$other = array()){
        $price = $order_info['paymoney'];
        $returnUrl    = $order_info['return_url'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/WeifuPayNotify';


        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'merchant_code' => $this->config['mch_id'],
            'service_type' => $order_info['service_type'],
            'notify_url' => $notifyUrl,
            'interface_version' => "V3.1",
            'client_ip' => get_client_ip(),
            'sign_type' => 'RSA-S',
             'order_no' => $order_sn,
             'order_time' => date("Y-m-d H:i:s" , time()),
             'order_amount' => $price,
            'product_name' => 'Changbao',
        );
        $ContentSign = $this->setParam($post_data,false); // 计算出Sign
        $post_data['sign'] = $ContentSign;
        $tar_url = 'https://api.wefupay.com/gateway/api/scanpay?';
        $str = http_build_query($post_data);
        $rest = https_request($tar_url . $str);
        $arrs = $this->xmlToArray($rest);
        return (!empty($arrs['response']['qrcode'])) ? urldecode($arrs['response']['qrcode']) : '';
    }

    private function setParam($param,$IS_Notify){
        $str = "";
        unset($param['PHPSESSID']);
        ksort($param);
        foreach($param as $key1=>$val)  {
            if($key1 == 'sign_type' || ($IS_Notify && $key1 == 'sign')) continue;
            $str.=$key1."=".$val."&";
        }
        $str = rtrim($str,'&');
        if(!$IS_Notify) {
            return $this->rsaSign($str);
        }else{
            return $this->VerifySign($str ,$param['sign']);
        }
        return false;
    }

    //数组转XML
    private function xmlToArray($xml){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }


    public function rsaSign($str = ''){
        $merchant_private_key = openssl_get_privatekey(file_get_contents(getcwd().'/ThinkPHP/Library/Vendor/WeifuCret/'. $this->config['mch_id'] .'_private_key.pem'));
        openssl_sign($str,$sign_info,$merchant_private_key,OPENSSL_ALGO_MD5);
        $sign = base64_encode($sign_info);
        return $sign;
    }

    public function VerifySign($signStr = "" , $dinpaySign){
        $dinpaySign = base64_decode($dinpaySign);
        $dinpay_public_key = openssl_get_publickey(file_get_contents(getcwd().'/ThinkPHP/Library/Vendor/WeifuCret/'. $this->config['mch_id'] .'_public_key.pem'));
        $flag = openssl_verify($signStr,$dinpaySign,$dinpay_public_key,OPENSSL_ALGO_MD5);
        return $flag;
    }


    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $MySign = $this->setParam($data,true);
        if($MySign){
            if($data['trade_status'] == "SUCCESS"){
                return true;
            }else{
                // 失败
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


}
